#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Author          : ricsy
@File            : translate.py
@Create Time     : 2025/6/18 05:09
@Last Modified   :
@Description     :
"""
from theme_customizer.base import BaseCustomizer


TRANSLATIONS = {
    "Repository": "查看仓库",
    "Show source": "显示源码",
    "Suggest edit": "修改建议",
    "Open issue": "创建议题",
    "Source repositories": "源码库",
}


class TranslatorManager(BaseCustomizer):

    def translate_ui(self, context):
        """翻译界面元素"""
        header_buttons = context.get("header_buttons", None)
        if header_buttons is None:
            return

        for button in context["header_buttons"]:
            if isinstance(button, dict):
                self._translate_button(button)

    @staticmethod
    def _translate_button(button):
        # 获取 group 分组按钮
        if button.get("type") == "group":
            for sub_button in button.get("buttons", []):
                if sub_button.get("type") == "link":
                    # 翻译按钮文本
                    for key in ["text", "tooltip"]:
                        if sub_button.get(key) in TRANSLATIONS:
                            sub_button[key] = TRANSLATIONS[sub_button[key]]
                    if "tooltip" in sub_button:
                        # 移除 tooltip 提示（因为不太好看）
                        del sub_button["tooltip"]
        else:
            for key in ["text", "tooltip"]:
                if button.get(key) in TRANSLATIONS:
                    button[key] = TRANSLATIONS[button[key]]
